package com.ucs.ui;

import com.ucs.util.UCSData;
import com.ucs.util.UCSUtil;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class DownloadActivity extends SimpleActivity {
    private static final String TAG = "ucs";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.download);
		
        final EditText sn_edittext = (EditText) findViewById(R.id.server_name);
        sn_edittext.setText(UCSUtil.getServername());

        final Context thisContext = this;
		Button downloadButton = (Button)findViewById(R.id.download);
		final Button signinButton = (Button)findViewById(R.id.signin);
		signinButton.setVisibility(View.INVISIBLE);
		downloadButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.v(TAG, "DownloadActivity:download:onClick");
                UCSUtil.setServername(sn_edittext.getText().toString());
                String catMsg = UCSUtil.downloadCategories();
                showMessage(thisContext, catMsg);                
                String userMsg = UCSUtil.downloadUsers();
                showMessage(thisContext, userMsg);
                if (UCSData.getInstance().getUsers().size()>0){
                	signinButton.setVisibility(View.VISIBLE);
                }
            }

			private void showMessage(final Context thisContext, String s) {
				Toast toast = Toast.makeText(thisContext, s, 2000);
				toast.show();
			}
		});
		signinButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
				launchSubActivity(SigninActivity.class, null, null);
			}
		});
	}
}
